package 普通Socket.Server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server1 {
	private static ServerSocket serverSocket = null;
	private static BufferedReader in = null;
	private static PrintWriter out = null;
	static final int servPort = 8585;

	public static void main(String[] args) throws IOException {

		serverSocket = new ServerSocket(servPort);
		System.out.println("The Server is start: " + serverSocket);
		while (true) {
			Socket socket = serverSocket.accept();
			try {
				System.out.println("Accept the Client: " + socket);
				// 设置IO句柄
				in = new BufferedReader(new InputStreamReader(socket
						.getInputStream()));
				out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(socket.getOutputStream())), true);
				while (true) {
					String str = in.readLine();
					if ("byebye".equals(str)) {
						break;
					}
					System.out.println("In Server reveived the info: " + str);
					out.println(str);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
//				System.out.println("close the Server socket and the io.");
//				socket.close();
				// serverSocket.close();
			}
		}
	}

}
